package com.hospital.register.core.controller.api;

import com.hospital.register.common.exception.BusinessException;
import com.hospital.register.common.result.Result;
import com.hospital.register.common.result.ResultCodeEnum;
import com.hospital.register.constants.BaseEntityConstants;
import com.hospital.register.core.service.DepartmentService;
import com.hospital.register.core.service.HospitalSetService;
import com.hospital.register.core.service.ScheduleService;
import com.hospital.register.dto.core.api.ScheduleRemoveDto;
import com.hospital.register.dto.core.api.ScheduleSaveDto;
import com.hospital.register.dto.core.common.SchedulePageDto;
import com.hospital.register.model.core.Schedule;
import com.hospital.register.vo.core.common.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Api(tags = "排班api", description = "面向医院")
@RestController
@RequestMapping("/api/hosp")
//@CrossOrigin
public class ScheduleApiController {

    /**
     * 处理排班业务
     */
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 处理医院设置相关接口
     */
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 处理科室相关业务
     */
    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "保存排班")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(@Validated ScheduleSaveDto dto,
                               @ApiIgnore HttpServletRequest request) {
        this.hospitalSetService.checkSignKey(dto.getHoscode(), dto.getSign(), request);

        if (!this.departmentService.exists(dto.getHoscode(), dto.getDepcode())) {//所属科室不存在
            throw new BusinessException("抱歉，所属科室不存在", ResultCodeEnum.PARAM_ERROR.getCode());
        }

        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(dto, schedule);

        Date now = new Date();
        schedule.setIsDeleted(BaseEntityConstants.NOT_DELETED);//未删除
        schedule.setUpdateTime(now);//更新时间

        if (this.scheduleService.existed(dto.getHoscode(), dto.getHosScheduleId())) {
            this.scheduleService.update(schedule);
        } else {
            schedule.setCreateTime(now);//创建时间
            this.scheduleService.insert(schedule);
        }

        return Result.ok();
    }

    @ApiOperation(value = "分页查询排班")
    @PostMapping("/schedule/list")
    public Result<Page<ScheduleQueryVo>> findSchedule(@Validated SchedulePageDto dto,
                                                      @ApiIgnore HttpServletRequest request) {
        this.hospitalSetService.checkSignKey(dto.getHoscode(), dto.getSign(), request);
        Page<ScheduleQueryVo> page = this.scheduleService.findPage(dto);
        return Result.ok(page);
    }

    @ApiOperation(value = "删除排班")
    @PostMapping("/schedule/remove")
    public Result remove(@Validated ScheduleRemoveDto dto,
                         @ApiIgnore HttpServletRequest request) {
        this.hospitalSetService.checkSignKey(dto.getHoscode(), dto.getSign(), request);
        boolean removed = this.scheduleService.remove(dto);

        if (removed) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


}
